package com.stx.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.stx.entity.User;
import com.stx.entity.User_collection;
import com.stx.service.UserCollectionService;
import com.stx.service.UserService;

public class Collection extends ActionSupport {

	private UserCollectionService ucs;
	private User user;
	private User_collection userColle;
	private UserService us;
    private String email;
    private String timeurl;
	
	
  	public String getTimeurl() {
  		return timeurl;
  	}

  	public void setTimeurl(String timeurl) {
  		this.timeurl = timeurl;
  	}
    
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public UserService getUs() {
		return us;
	}

	public void setUs(UserService us) {
		this.us = us;
	}

	public UserCollectionService getUcs() {
		return ucs;
	}

	public void setUcs(UserCollectionService ucs) {
		this.ucs = ucs;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User_collection getUserColle() {
		return userColle;
	}

	public void setUserColle(User_collection userColle) {
		this.userColle = userColle;
	}

	/*
	 * 得到用户 更新session
	 */
	public String delCollection() {
        if(userColle==null){
        	addActionError("删除失败,请正确操作");
			SimpleDateFormat sdf=new SimpleDateFormat("HH-mm-ss");
			timeurl=sdf.format(new Date());
			System.out.println("非法删除收藏......");
			return "error";
        }
		userColle = ucs.getCollection(userColle);
		user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");
		if (user == null) {

			addActionError("权限丢失");
			return "sessionerror";
		}
		if (userColle != null) {
			ucs.delCollection(userColle);
			user = us.getUserInformation(user);
			ServletActionContext.getRequest().getSession()
					.setAttribute("user",user);
			email=user.getUs_email();
			addActionError("删除一个收藏成功");
			SimpleDateFormat sdf=new SimpleDateFormat("HH-mm-ss");
			timeurl=sdf.format(new Date());
			return "success";
		} else {
			email=user.getUs_email();
			addActionError("删除失败,请正确操作");
			SimpleDateFormat sdf=new SimpleDateFormat("HH-mm-ss");
			timeurl=sdf.format(new Date());
			System.out.println("非法删除收藏"+user.getUs_email());
			return "error";
		}
	}
	
		
		
}
